package com.mystudy.navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {

    private List<MenuItem> menuItems;
    private int selectedPosition = -1;
    private OnNavigationItemClickListener listener;

    public NavigationAdapter() {
        this.menuItems = new ArrayList<>();
    }

    public void setOnNavigationItemClickListener(OnNavigationItemClickListener listener) {
        this.listener = listener;

    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
        notifyDataSetChanged();
    }

    public void addNavigationItem(MenuItem menuItem) {
        menuItems.add(menuItem);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_navigation_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MenuItem menuItem = menuItems.get(position);
        holder.bind(menuItem, position);
    }

    @Override
    public int getItemCount() {
        return menuItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView iconImageView;
        private TextView titleTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iconImageView = itemView.findViewById(R.id.tv_home_navigation_icon);
            titleTextView = itemView.findViewById(R.id.tv_home_navigation_title);
            itemView.setOnClickListener(this);
        }

        public void bind(MenuItem menuItem, int position) {
            iconImageView.setImageResource(menuItem.getIconResId());
            titleTextView.setText(menuItem.getTitle());
            itemView.setSelected(selectedPosition == position);
            Log.d("NavigationAdapter", "Item clicked: " + position);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                if (listener != null) {
                    listener.onNavigationItemClick(position);
                }
            }
        }
    }

    public interface OnNavigationItemClickListener {
        void onNavigationItemClick(int position);
    }

    public static class MenuItem {
        private int iconResId;
        private String title;

        public MenuItem(int iconResId, String title) {
            this.iconResId = iconResId;
            this.title = title;
        }

        public int getIconResId() {
            return iconResId;
        }

        public String getTitle() {
            return title;
        }
    }
}